package com.example.hbutliving.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.example.hbutliving.config.WeiXinConfig;
import com.example.hbutliving.domain.receive.rails.UserId;
import com.example.hbutliving.domain.receive.rails.UserToken;
import com.example.hbutliving.domain.receive.weixin.AccessToken;
import com.example.hbutliving.domain.receive.weixin.WinXinResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * jwt工具类
 *
 * @author lizijian
 */
@Data
@Component
@Slf4j
public class JwtUtil {

  private static String secret = "fnsfjaiosjfsa5166sg1s6g";

  /**
   * token过期时间15天
   */
  private static long expireTime = TimeUnit.DAYS.toMillis(15);

  @Autowired
  private RestTemplate restTemplate;

  @Value("${rails_url}")
  private String railsUrl;

  public String getTokenUrl(){
    return railsUrl + "/rails/local/encode_token";
  }

  public String verifyTokenUrl(){
    return railsUrl + "/rails/local/decode_token";
  }

  public String createManagerToken(int id, String username, String password) {
    String token = JWT.create().withExpiresAt(new Date(System.currentTimeMillis() + expireTime))
        .withClaim("id", id)
        .withClaim("username", username)
        .withClaim("password", password).sign(Algorithm.HMAC256(secret));
    return token;
  }

  public String createUserToken(int id) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getTokenUrl())
            .queryParam("user_id", id);
    ResponseEntity<UserToken> entity = restTemplate.getForEntity(builder.toUriString(), UserToken.class);
    log.info("body" + entity.getBody().toString());
    log.info("token:" + Objects.requireNonNull(entity.getBody()).getToken());
    return Objects.requireNonNull(entity.getBody()).getToken();
  }


  public Map<String, Claim> verify(String token){
    DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
    return decodedJWT.getClaims();
  }

  public Integer verifyUserId(String token){
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(verifyTokenUrl())
            .queryParam("token", token);
    ResponseEntity<UserId> entity = restTemplate.getForEntity(builder.toUriString(), UserId.class);
    log.info("body" + entity.getBody().toString());
    log.info("token verify" + Objects.requireNonNull(entity.getBody()).getUserId());
    return Objects.requireNonNull(entity.getBody()).getUserId();
  }

}
